# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/16 19:33
# @Author   : Robot-Zsj
"""
description:

i:
5
1,2
1,1,0,1,0
1,1,0,0,0
0,0,1,0,1
1,0,0,1,0
0,0,1,0,1

o:
3
"""


class UnionFindSet:
    def __init__(self, n):
        self.fa = [i for i in range(n)]

    def find(self, x):
        if x != self.fa[x]:
            self.fa[x] = self.find(self.fa[x])
            return self.fa[x]
        return x

    def union(self, x, y):
        x_fa = self.find(x)
        y_fa = self.find(y)

        if x_fa != y_fa:
            self.fa[y_fa] = x_fa


n = int(input())
confirmed = list(map(int, input().split(",")))
matrix = [list(map(int, input().split(","))) for _ in range(n)]


def getResult():
    ufs = UnionFindSet(n)

    for i in range(n):
        for j in range(i, n):
            if matrix[i][j] == 1:
                ufs.union(i, j)

    cnts = [0] * n
    for i in range(n):
        fa = ufs.find(i)
        cnts[fa] += 1

    confirmed_fa = set()

    ans = 0

    for i in confirmed:
        fa = ufs.find(i)

        if fa in confirmed_fa:
            continue
        confirmed_fa.add(fa)
        ans += cnts[fa]

    return ans - len(confirmed)


print(getResult())
